#include <QtCore/QCoreApplication>

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>

#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

#include "Client.h"
#include "FieldPlayer.h"
#include "Trainer.h"
#include "Parser.h"
#include "ServerParam.h"
#include "PlayerParam.h"

using namespace std;


void signal_catcher(int sig)
{
        signal(sig, signal_catcher);

        std::cerr << "catch signal " << sig << std::endl;

        std::cerr << "exiting safely now" << std::endl;

        exit(0);
}


void RegisterSignalHandler();

int main(int argc, char* argv[])
{
    //printf("Begin\n");

    //QCoreApplication a(argc, argv);

    RegisterSignalHandler();

    if (argc < 4)
    {
        printf("Error in usage");
        return 0;
    }

    //printf("Here\n");
    ServerParam::instance().init(argc, argv);
    PlayerParam::instance().init(argc, argv);

    Client *client;

    if (PlayerParam::instance().getPlayerType() == M_PLAYER)
    {
        client = new FieldPlayer();
    }
    else
    {
        client = new Trainer();
    }

    delete client;

    //return a.exec();
    return 0;
}


void RegisterSignalHandler()
{
        if(signal(SIGINT, signal_catcher) == SIG_ERR)
                perror("SIGINT"), exit(1);
        if(signal(SIGQUIT, signal_catcher) == SIG_ERR)
                perror("SIGQUIT"), exit(1);
        if(signal(SIGSEGV, signal_catcher) == SIG_ERR)
                perror("SIGSEGV"), exit(1);
        if(signal(SIGILL, signal_catcher) == SIG_ERR)
                perror("SIGKILL"), exit(1);
        if(signal(SIGFPE, signal_catcher) == SIG_ERR)
                perror("SIGFPE"), exit(1);
}


